<?php

require_once("IController.php");

class ControllerBase implements IController {
    public $Smarty;
    public $Language;
    public $Id;
    public $ControllerName;
    public $ActionName;
    public $Template;
    public $ConfigFile;
    private $User;
    private $IsUserAuthenticationRequired = false;
    protected $PageTitle = "Not_Set";

    function __construct($smarty, $baseDir, $controllerName, $action, $language, $id){
        $this->Smarty = $smarty;
        $this->BaseDir = $baseDir;
        $this->ControllerName = $controllerName;
        $this->ActionName = $action;
        $this->Language = $language;
        $this->Id = $id;

        $nameFormat = $this->ControllerName.".".$this->ActionName;
        $this->Template = $nameFormat.".tpl";
        $this->ConfigFile  = $nameFormat.".conf";

        $this->User = new UserBase($this->Smarty, $this->Language);

        if($this->Smarty->debugging){
            $debugInfo["Template"] = $this->Template;
            $debugInfo["ConfigFile"] = $this->ConfigFile;
            $di["ControllerBase_DEBUG_INFO"][] = $debugInfo;
            echo "<pre>";
            print_r($di);
            echo "</pre>";
        }
    }

    public function IsPostBack() {
        return (count($_POST) > 1);
    }

    public function Resource($key, $arg = "") {
        return $this->Smarty->FetchFromDb($key, $this->Language, $arg);
    }

    public function IsUserAuthenticationRequired($value = null) {
        if(isset($value)){
            $this->IsUserAuthenticationRequired = $value;
        }
        return $this->IsUserAuthenticationRequired;
    }

    //region Master sections

    public function User() {
        return $this->User;
    }

    //region page life cycle

    public function OnInit() {
        $this->InitBreadCrumbs();
        $this->Smarty->assign(IController::MasterBody, $this->Action($this->ActionName));
    }

    public function OnLoad(){
        $this->Smarty->assign(IController::MasterTitle, $this->Title());
        $this->Smarty->assign(IController::MasterScripts, $this->Script());
        $this->Smarty->assign(IController::MasterNavigation, $this->Navigation());
        $this->Smarty->assign(IController::MasterQuickNavigation, $this->QuickNavigation());
        $this->Smarty->assign(IController::MasterHeader, $this->Header());
        $this->Smarty->assign(IController::MasterFooter, $this->Footer());
    }

    public function OnRender() {
        $this->SetBreadCrumbs();
        $this->Smarty->display(IController::MasterPageTPL);
        $this->User()->KeepAlive();
    }

    public function Execute() {
        $this->OnInit();
        $this->OnLoad();
        $this->OnRender();
    }

    protected function Action($action) {
        $this->$action();
        if($this->IsForbidden()){
            $this->Info($this->Resource("TXT_INFO_LOGIN_REQUESTED"));
            return $this->Fetch("User.Login.tpl");
        }
        return $this->Fetch();
    }

    //endregion page life cycle

    protected function Title($row = null) {
        if($row != null){
            $this->PageTitle = $row;
            $this->BreadCrumbs($row);
        }
        return $this->MasterStringCommon("title", $row, " - ");
    }

    protected function Header($row = null) {
        return $this->MasterStringCommon("header", $row);
    }

    protected function Navigation($row = null) {
        return $this->MasterStringCommon("navigation", $row);
    }

    protected function QuickNavigation($row = null) {
        return $this->MasterStringCommon("quickNavigation", $row);
    }

    protected function Footer($row = null) {
        return $this->MasterStringCommon("footer", $row);
    }

    protected function Script($row = null) {
        return $this->MasterStringCommon("scripts", $row);
    }

    protected function BreadCrumbs($row = null) {
        return $this->MasterArrayCommon("breadCrumbs", $row);
    }

    private function MasterStringCommon($key, $row = null, $separator = "\n") {
        $value = isset($this->$key) ? $this->$key : null;
        if($row != null){
            $value = ($value != null ? $value.$separator.$row : $row);
            $this->$key = $value;
        }
        return $value;
    }

    private function MasterArrayCommon($key, $row = null) {
        $value = isset($this->$key) ? $this->$key : Array();
        if($row != null){
            $value[] = $row;
            $this->$key = $value;
        }
        return $value;
    }

    protected function IsForbidden() {
        return ($this->IsUserAuthenticationRequired() && !$this->User()->IsLoggedIn());
    }

    protected function Fetch($template = null){
        $template = isset($template) ? $template : $this->Template;
        if(defined('BASE_TEMPLATES_DIR')){
            $templateAtBase = BASE_TEMPLATES_DIR.$template;
            if (file_exists($templateAtBase) && is_file($templateAtBase)) {
                return $this->Smarty->fetch($templateAtBase);
            }
        }

        return $this->Smarty->fetch($template);
    }

    protected function SetAuthentication(){
        $this->QuickNavigation($this->Fetch("ui_login.tpl"));
    }

    protected function SetBreadCrumbs(){
        if(!$this->IsForbidden()){
            $this->Smarty->assign("breadCrumbs", $this->BreadCrumbs());
        }
        $this->Smarty->assign(IController::MasterBreadCrumbs, $this->Fetch("ui_breadcrumbs.tpl"));
    }

    //endregion Master sections

    //region message

    public function Error($error = null){
        return $this->User()->Error($error);
    }

    public function Info($info = null){
        return $this->User()->Info($info);
    }

    public function Warning($warning = null){
        return $this->User()->Warning($warning);
    }

    //endregion message

    //region BreadCrumbs

    public function InitBreadCrumbs(){
        $default = "Index";
        if($this->ControllerName != $default){
            $this->BreadCrumbs($this->BreadCrumb($default, $this->ControllerName));
        }
        if($this->ActionName != $default){
            $this->BreadCrumbs($this->BreadCrumb($this->ActionName, $this->ControllerName));
        }
    }

    private function BreadCrumb($action, $controller) {
        $resourceIndex = "TXT_CTRL_".$controller."_".$action."_TITLE";
        return "<a href='/".$this->BaseDir."/".$this->Language."/".$controller."/".$action."/'>".$this->Resource($resourceIndex)."</a>";
    }

    //endregion BreadCrumbs

}
